package org.asiainfo.ability.manager.service;

import org.asiainfo.ability.base.utils.SeqUtils;
import org.asiainfo.ability.base.utils.Sequence;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: visen
 * @Date: 2018/3/11
 * @Description:
 */
@Component
public class SeqService {

    public static enum SeqTypes {
        ABILITY, SERVICE, INSTANCE, MACHINE, USER, JAR
    }
    private final Map<SeqTypes, Sequence> seqMap;
    public SeqService() {
        seqMap = new HashMap();
    }
    @PostConstruct
    private void init() {
        for(SeqTypes type:SeqTypes.values()) {
            seqMap.put(type, SeqUtils.newInstance());
        }
    }

    public long next(SeqTypes type) {
        Sequence seqUtils = seqMap.get(type);
        if(seqUtils == null) {
            throw new IllegalArgumentException("seq type not defined");
        }
        return seqUtils.id();

    }
}
